package org.xteam.engine.geom;

import java.awt.Point;

import org.xteam.engine.Camera;

public class Point3D {
	
	public double x;
	public double y;
	public double z;
	public double m;
	
	public Point3D(double x, double y, double z) {
		this(x, y, z, 1.0);
	}

	public Point3D(double x, double y, double z, double m) {
		this.x = x;
		this.y = y;
		this.z = z;
		this.m = m;
	}

	public Point3D substract(Point3D p) {
		return new Point3D(x - p.x, y - p.y, z - p.z);
	}

	public Point3D normalize() {
		double m = module();
		return new Point3D(x / m, y / m, z / m);
	}

	private double module() {
		return Math.sqrt(x*x + y*y + z*z);
	}

	public Point3D crossProduct(Point3D p) {
		return new Point3D(y * p.z - z * p.y, z * p.x - x * p.z, x * p.y - y * p.x);
	}

	public Point project(Camera camera) {
		return new Point(
				(int)(camera.getFocal() * camera.getScaleX() * x / (m * z)) + camera.getOrigin().x,
				(int)(camera.getFocal() * camera.getScaleY() * y / (m * z)) + camera.getOrigin().y);
	}
}
